package thriftx.demo.provider;

import org.apache.thrift.TException;
import thriftx.demo.api.CalculatorService;
import thriftx.demo.api.Work;

public class CalculatorHandler implements CalculatorService.Iface {

    public int calculate(int logid, Work work) throws TException {
        System.out.println("calculate(" + logid + ", {" + work.op + "," + work.num1 + "," + work.num2 + "})");
        int val = 0;
        switch (work.op) {
            case ADD:
                val = work.num1 + work.num2;
                break;
            case SUBTRACT:
                val = work.num1 - work.num2;
                break;
            case MULTIPLY:
                val = work.num1 * work.num2;
                break;
            case DIVIDE:
                if (work.num2 == 0) {
                    throw new TException("Cannot divide by 0");
                }
                val = work.num1 / work.num2;
                break;
            default:
                throw new TException("Unknown operation");
        }
        return val;
    }

}
